//*********************************************************************
//		Copyright (c) 2003 - 2008 Microsoft Corporation.
//      This code is provided under Microsoft Public License, which can be found at
//      http://www.microsoft.com/resources/sharedsource/licensingbasics/publiclicense.mspx 
//
// @File: ProjectInstaller.cs
//
// Purpose: Installer
//
//
//
// Notes: 
//
//
//	
// History: 
//     @Version: V2.1
//
//     10/31/08 PGV 2.1.001.0 Starting V2.1 (based on 2.0.002.0 code)
//
//
// @EndHeader@
//*********************************************************************

using System;
using System.Collections;
using System.ComponentModel;
using System.Configuration.Install;
using Microsoft.Win32;
using System.IO;
using System.Diagnostics;

namespace PerfService
{
	[RunInstaller(true)]
	public class ProjectInstaller : System.Configuration.Install.Installer
	{
		private PerfService.SvcInstaller serviceProcessInstaller1;
		private System.ServiceProcess.ServiceInstaller serviceInstaller1;
		private System.Diagnostics.EventLogInstaller eventLogInstaller1;
		private RInstaller registrar;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public ProjectInstaller()
		{
			// This call is required by the Designer.
			InitializeComponent();
		}

		#region Component Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            this.serviceProcessInstaller1 = new PerfService.SvcInstaller();
            this.serviceInstaller1 = new System.ServiceProcess.ServiceInstaller();
            this.eventLogInstaller1 = new System.Diagnostics.EventLogInstaller();
            this.registrar = new PerfService.RInstaller();
            // 
            // serviceProcessInstaller1
            // 
            this.serviceProcessInstaller1.Password = null;
            this.serviceProcessInstaller1.Username = null;
            // 
            // serviceInstaller1
            // 
            this.serviceInstaller1.DisplayName = "Microsoft SQLH2 Performance Collector";
            this.serviceInstaller1.ServiceName = "SQLH2PerfCollector";
            this.serviceInstaller1.StartType = System.ServiceProcess.ServiceStartMode.Automatic;
            this.serviceInstaller1.AfterInstall += new System.Configuration.Install.InstallEventHandler(this.serviceInstaller1_AfterInstall);
            // 
            // eventLogInstaller1
            // 
            this.eventLogInstaller1.CategoryCount = 0;
            this.eventLogInstaller1.CategoryResourceFile = null;
            this.eventLogInstaller1.Log = "Application";
            this.eventLogInstaller1.MessageResourceFile = null;
            this.eventLogInstaller1.ParameterResourceFile = null;
            this.eventLogInstaller1.Source = "SQLH2PerfCollector";
            // 
            // ProjectInstaller
            // 
            this.Installers.AddRange(new System.Configuration.Install.Installer[] {
            this.serviceProcessInstaller1,
            this.serviceInstaller1,
            this.eventLogInstaller1,
            this.registrar});

		}
		#endregion

        private void serviceInstaller1_AfterInstall(object sender, InstallEventArgs e)
        {

        }
	}

	class SvcInstaller : System.ServiceProcess.ServiceProcessInstaller
	{
		public override void Install (IDictionary mySavedState)
		{
			string b = this.Context.Parameters["SvcAccount"];
			if (b == "1")
			{
				this.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
			}
			else
			{
				this.Account = System.ServiceProcess.ServiceAccount.User;
				this.Password = null;
				this.Username = null;
			}

			base.Install (mySavedState);
		}
	}

	class RInstaller : System.Configuration.Install.Installer 
	{
		public override void Install (IDictionary mySavedState)
		{
			base.Install( mySavedState );

			Guid iGUID = Guid.NewGuid ();
			RegistryKey rk;
			rk = (Registry.LocalMachine).OpenSubKey ("SOFTWARE", true); 
			rk = rk.CreateSubKey ("Microsoft");
			rk = rk.CreateSubKey ("SQLH2PerfCollector");
			rk.SetValue ("iGUID", iGUID.ToString ());

			string OutputDir = this.Context.Parameters["OutputDir"];
			string BinDir = this.Context.Parameters["BinDir"];
			if (OutputDir.StartsWith ("\\", StringComparison.Ordinal))
			{
				OutputDir = BinDir+OutputDir.Substring (1);
			}
			if (!Directory.Exists (OutputDir)) Directory.CreateDirectory (OutputDir);
			rk.SetValue ("OutputDir", OutputDir);//+"Data"

		}

		public override void Uninstall( IDictionary mySavedState )
		{  
			RegistryKey rk, sk;
			string OutputDir;

			if (mySavedState == null)
			{
				// Uninstallation Error!
			}
			else
			{
				rk = (Registry.LocalMachine).OpenSubKey ("SOFTWARE", true);
                rk = rk.OpenSubKey("Microsoft", true);
				try
				{
					sk = rk.OpenSubKey ("SQLH2PerfCollector");
					OutputDir = (string)sk.GetValue ("OutputDir");
					Directory.Delete (OutputDir);
				} 
				catch {}
 
                rk.DeleteSubKey ("SQLH2PerfCollector");

				base.Uninstall( mySavedState );
			}
		}

	}
}

